/**
 * 链上数据
 * 用于轮播
 * */
import {deepCopy} from './deepCopy'
import $Axios from 'axios'
import {filterKey} from "./key_filter";

const PageSide = 16  // 分页每页显示8条
const ShowCount = 8 // 轮播框显示的数量
export default {
    data() {
        return {

            handleChain: [],
            currentPage: 1,
            recordsData: null,
            blockData: [],
            replaceD: [],
            nextStatus: false, // 下一页数据
        }
    },
    mounted() {
        this.init()
    },
    methods: {
        async init() {
            await this.cache()
            await this.replacePage()
        },
        // 缓存下一页的数据
        async nextPage() {
            await this.cache();
        },
        async cache() {
            const response = await $Axios.post('/api/statistic/block/page',{
                page: this.currentPage,
                size: PageSide,
                direction: 'desc'
            })

            this.recordsData = response.data.data.list
            const {size, pages} = response.data.data
            // pageNum 当前页
            // pages 总共有多少页
            // size  实际当前页返回条数
            if (this.recordsData.length < PageSide) {
                const merge_response = await $Axios.post('/api/statistic/block/page',{
                    page: 1,
                    size: PageSide - size,
                    direction: 'desc'
                })
                this.currentPage = 0
                this.recordsData.concat(merge_response.data.data.list || [])

            }
            // 走到最后页，并且没有初始化0，下次watch handler修改current
            if (this.currentPage !== 0 || this.currentPage === pages) this.currentPage = 0

            const r = this.recordsData
            if (this.blockData.length > 0) { // 非初始化
                const copySwiper = deepCopy(this.blockData).slice(this.blockData.length - ShowCount , this.blockData.length)
                let prev = deepCopy(this.blockData)
                for (let j = 0; j < r.length; j++) {
                    prev[j + ShowCount] = r[j]
                    if (j < ShowCount) {
                        prev[j] = copySwiper[j]
                    }
                }
                this.replaceD = prev
            } else {
                this.replaceD = r
            }
        },
        // 替换下一页数据
        async replacePage() {
            this.blockData = this.replaceD
            if (!this._first) {
                this._first = true
                this.blockChain = filterKey(this.blockData[this.swiperIndex])
                this.visibility = true
            }
            this.$refs.mySwiper.swiper.update();
            this.$refs.mySwiper.swiper.slideTo(0,0);
            this.$refs.mySwiper.swiper.autoplay.start();
        },
    },
    watch: {
        swiperIndex: {
            handler(nextNumber) {
                if (nextNumber === 1) {
                    this.currentPage += 1
                    this.nextPage()
                } else if (nextNumber === ShowCount && nextNumber + ShowCount === this.blockData.length && !this.nextStatus) {
                    this.nextStatus = true

                    this.replacePage()
                } else if (nextNumber === PageSide && nextNumber + ShowCount === this.blockData.length) {

                    this.replacePage()
                }
            },
            deep: true
        }
    },
}
